package com.banmao.snake_server.netty.handler;

import com.banmao.snake_core.netty.message.impl.LoginRequestMessage;
import com.banmao.snake_core.netty.message.impl.LoginResponseMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginRequestMessage msg) throws Exception {
        log.info("服务端接收到消息: {}", msg);
        LoginResponseMessage loginResponseMessage = new LoginResponseMessage();
        Thread.sleep(10);
        loginResponseMessage.setCode(200);
        loginResponseMessage.setSequenceId(msg.getSequenceId());



        if("123".equals(msg.getPassword())) {
            loginResponseMessage.setSuccess(false);
            loginResponseMessage.setCause("密码错误");
        } else {
            loginResponseMessage.setSuccess(true);
            loginResponseMessage.setResponse("登陆成功");
        }
        ctx.channel().writeAndFlush(loginResponseMessage);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("服务端异常 msg:{}, error:", cause.getMessage(), cause);
    }



}
